<?php
namespace Api\Model;
use Think\Model;
class UserModel extends Model {
    protected $tableName = 'user';
    public $inviter_id=0;
    public $code="";
    public function add(){
        $data=$this->data;
        $model = new Model();
        $model->startTrans();
        $flag=false;
        $res=$model->table(C('DB_PREFIX').'user')->where("id=".$this->id)->add($data);
        $user_id=$model->getLastInsID();
        if($res){
            $user_kd=50;
            $yaoqing_kd=30;
            $pingtai_kd=20;
            //添加余额数据   新用户注册  获得50咖点
            $add_user=$model->table(C('DB_PREFIX').'user_balance')->add([
                'total_balance'=>$user_kd,
                'balance'=>$user_kd,
                'updatedtime'=>time(),
                'user_id'=>$user_id,
                'username'=>$data['username'],
                'createdtime'=>time()
            ]);
            $add_kadian=$model->table(C('DB_PREFIX').'user_balance_log')->add([
                'user_id'=>$user_id,
                'type'=>0,
                'current_balance'=>0,
                'balance'=>$user_kd,
                'total_balance'=>$user_kd,
                'createdtime'=>time(),
                'description'=>'注册新用户获得：'.$user_kd." 咖点"
            ]);

            //邀请人获得咖点
            $inviter=$model->table(C('DB_PREFIX')."user_balance")->where("user_id=".$this->inviter_id)->find();
            $add_inviter=$model->table(C('DB_PREFIX').'user_balance')->where("user_id=".$this->inviter_id)->save([
                'total_balance'=>$inviter['total_balance']+$yaoqing_kd,
                'balance'=>$inviter['balance']+$yaoqing_kd,
                'updatedtime'=>time(),
            ]);
            $add_inviter_kadian=$model->table(C('DB_PREFIX').'user_balance_log')->add([
                'user_id'=>$inviter['user_id'],
                'type'=>0,
                'current_balance'=>$inviter['balance'],
                'balance'=>$yaoqing_kd,
                'total_balance'=>$inviter['balance']+$yaoqing_kd,
                'createdtime'=>time(),
                'description'=>'邀请用户：'.$data['username']." 获得：".$yaoqing_kd." 咖点"
            ]);

            //平台获得咖点
            $pingtai=$model->table(C('DB_PREFIX')."user_balance")->where("user_id=1")->find();
            $add_pingtai=$model->table(C('DB_PREFIX').'user_balance')->where("user_id=1")->save([
                'total_balance'=>$pingtai['total_balance']+($user_kd+$yaoqing_kd+$pingtai_kd),
                'balance'=>$pingtai['balance']+$pingtai_kd,
                'updatedtime'=>time(),
            ]);
            $add_pingtai_kadian=$model->table(C('DB_PREFIX').'user_balance_log')->add([
                'user_id'=>$pingtai['user_id'],
                'type'=>0,
                'current_balance'=>$pingtai['balance'],
                'balance'=>$pingtai_kd,
                'total_balance'=>$pingtai['balance']+$pingtai_kd,
                'createdtime'=>time(),
                'description'=>'注册新用户：'.$data['username']." 获得：".$pingtai_kd." 咖点"
            ]);
            if($add_user && $add_kadian && $add_inviter && $add_inviter_kadian &&  $add_pingtai &&  $add_pingtai_kadian ){
                $flag=SysCodeModel::deleteCode($data['username'],$this->code);
            }
         }
        if(!$flag){
            $model->rollback();
        }else{
            $model->commit();
        }
        return $flag;
    }
}